(function (global, factory) {
	typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('babel-runtime/core-js/json/stringify'), require('stat/vue')) :
	typeof define === 'function' && define.amd ? define(['exports', 'babel-runtime/core-js/json/stringify', 'stat/vue'], factory) :
	(factory((global.stat = {}),global._JSON$stringify,global.vueStat));
}(this, (function (exports,_JSON$stringify,vueStat) { 'use strict';

	_JSON$stringify = _JSON$stringify && _JSON$stringify.hasOwnProperty('default') ? _JSON$stringify['default'] : _JSON$stringify;
	vueStat = vueStat && vueStat.hasOwnProperty('default') ? vueStat['default'] : vueStat;

	var viewCount = 0;

	var GetSdkData = function GetSdkData(getData) {
		return function (dataList) {
			var sdkData = function sdkData() {
				var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : getData()[0];
				return {
					actionType: data.type,
					pageName: data.page,
					target: data.target,

					parentPage: (viewCount <= 1 && /^http/.test(data.parentPage) && data.other.pageChainMetaData[1] ? data.other.pageChainMetaData[1].statName : data.parentPage) || '/',
					pageFrom: data.other.pageStackMetaData.map(function (item) {
						return item.statName || '/';
					}).reverse(),
					actionParams: data.actionParams,
					pageParams: data.pageParams
				};
			};

			if (!Array.isArray(dataList)) return _JSON$stringify([sdkData()]);

			return _JSON$stringify(dataList.map(function (item) {
				return sdkData(item);
			}));
		};
	};

	exports.getSdkData = function getSdkData() {
		return true;
	};

	var clientCore = (function (router) {
		var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
		    _ref$showConsole = _ref.showConsole,
		    showConsole = _ref$showConsole === undefined ? false : _ref$showConsole,
		    _ref$oncePageView = _ref.oncePageView,
		    oncePageView = _ref$oncePageView === undefined ? true : _ref$oncePageView,
		    _ref$onceNodeView = _ref.onceNodeView,
		    onceNodeView = _ref$onceNodeView === undefined ? true : _ref$onceNodeView,
		    _ref$metaPageName = _ref.metaPageName,
		    metaPageName = _ref$metaPageName === undefined ? '' : _ref$metaPageName,
		    _ref$metaPageParamsGe = _ref.metaPageParamsGet,
		    metaPageParamsGet = _ref$metaPageParamsGe === undefined ? '' : _ref$metaPageParamsGe,
		    _ref$pageEndOptions = _ref.pageEndOptions;

		_ref$pageEndOptions = _ref$pageEndOptions === undefined ? {} : _ref$pageEndOptions;
		var _ref$pageEndOptions$p = _ref$pageEndOptions.pathFilter,
		    pathFilter = _ref$pageEndOptions$p === undefined ? [] : _ref$pageEndOptions$p,
		    _ref$pageEndOptions$h = _ref$pageEndOptions.h5ToClient,
		    h5ToClient = _ref$pageEndOptions$h === undefined ? [] : _ref$pageEndOptions$h,
		    _ref$unRouter = _ref.unRouter,
		    unRouter = _ref$unRouter === undefined ? false : _ref$unRouter,
		    _ref$statName = _ref.statName,
		    statName = _ref$statName === undefined ? document.title : _ref$statName;
		return {
			install: function install(Vue) {
				Vue.prototype.$vueStat = vueStat;

				var hostSdk = void 0;
				try {
					hostSdk = require('host-sdk');
				} catch (err) {
					hostSdk = {
						stat: function stat() {},
						setStatFun: function setStatFun() {},
						init: function init() {},
						getInfo: function getInfo() {},
						versionVerify: function versionVerify() {},
						uploadPageEndStatistics: function uploadPageEndStatistics() {}
					};
				}

				var stat = function stat() {
					var _hostSdk;

					showConsole && console.log('sdkData：', JSON.parse(arguments.length <= 0 ? undefined : arguments[0]), arguments.length <= 1 ? undefined : arguments[1]);

					return (_hostSdk = hostSdk).stat.apply(_hostSdk, arguments);
				};

				var getBaseData = function getBaseData() {
					return Vue.prototype.$Stat._getPageViewData();
				};

				exports.getSdkData = GetSdkData(getBaseData);

				vueStat.set({
					from: 'client',
					router: router,
					metaPageName: metaPageName,
					metaPageParamsGet: metaPageParamsGet,
					report: function report(data, opt) {
						return stat(exports.getSdkData(data), opt);
					},

					chainLength: 10,
					oncePageView: oncePageView,
					onceNodeView: onceNodeView,
					unRouter: unRouter,
					statName: statName
				});

				Vue.use(vueStat);

				var time = Date.now();
				var _time = 0;
				var diff = 0;
				var updateTime = function updateTime() {
					_time = Date.now();
					diff = _time - time;
					time = _time;

					return true;
				};
				var getDiffData = function getDiffData() {
					var isSdk = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
					var _diff = arguments[1];
					return { pageName: JSON.parse(exports.getSdkData())[0][isSdk ? 'pageName' : 'parentPage'], duration: _diff || diff };
				};

				var getChainParams = function getChainParams(index) {
					return Vue.prototype.$Stat._route.getOwnPageChainParams(null, { index: index })[0];
				};

				if (router) router.afterEach(function (to) {
					return setTimeout(function () {
						return viewCount++ && updateTime() && hostSdk.uploadPageEndStatistics(getDiffData());
					}, 100);
				});else {
						var lastPage = getChainParams(1);

						if (lastPage && lastPage.date) hostSdk.uploadPageEndStatistics(getDiffData(false, (getChainParams(0).date || Date.now()) - lastPage.date));
					}

				var getPath = function getPath() {
					return router ? router.currentRoute.path : window.location.pathname;
				};

				hostSdk.pause(function () {
					if (pathFilter.find(function (item) {
						return getPath().indexOf(item) > -1;
					})) return;

					updateTime() && hostSdk.uploadPageEndStatistics(getDiffData(true));
				});

				hostSdk.setStatFun(function () {
					if (h5ToClient.find(function (item) {
						return getPath().indexOf(item) > -1;
					})) updateTime() && hostSdk.uploadPageEndStatistics(getDiffData(true));

					return exports.getSdkData(getBaseData());
				});
			}
		};
	});

	exports.GetSdkData = GetSdkData;
	exports.default = clientCore;

	Object.defineProperty(exports, '__esModule', { value: true });

})));
